class Solution {
public:
    long long minTime(vector<int>& skill, vector<int>& mana) {
        int n = skill.size();
        vector<long long> finish(n);

        for (int m : mana) {
            long long sum = finish[0];
            for (int i = 0; i < n; i++)
                sum = max(sum, finish[i]) + skill[i] * m;

            finish[n - 1] = sum;
            for (int i = n - 2; i >= 0; i--)
                finish[i] = finish[i + 1] - skill[i + 1] * m;
        }

        return finish[n - 1];
    }
};